package com.example.controller.frontend;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.ApiResult;
import com.example.entity.Resource;
import com.example.service.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/api/frontend/resource")
@Api(tags = "资源管理")
public class FrontendResourceController {

    @javax.annotation.Resource
    private ResourceService resourceService;

    @PostMapping("/updateResource")
    @ApiOperation(value = "修改资源")
    public ApiResult upDateResource(HttpServletRequest request,
                                    @RequestParam Integer id,
                                    @RequestParam String resourceTitle,
                                    @RequestPart MultipartFile resourceUrl,
                                    @RequestPart MultipartFile picUrl,
                                    @RequestParam Integer majorId    ){
        Integer integer = resourceService.updateResource(request,id ,resourceTitle, resourceUrl, picUrl, majorId);
        return ApiResult.ok(integer);
    }

    @PostMapping("/addResource")
    @ApiOperation(value = "添加资源")
    public ApiResult addResource(
                                 @RequestParam String resourceTitle,
                                 @RequestPart MultipartFile resourceUrl,
                                 @RequestPart MultipartFile picUrl,
                                 @RequestParam Integer majorId    ){
        Integer integer = resourceService.addResource(resourceTitle, resourceUrl, picUrl, majorId);
        return ApiResult.ok(integer);
    }

    @DeleteMapping("/deleteResource")
    @ApiOperation(value = "删除资源")
    public ApiResult deleteResource(@RequestParam Integer id) {
        resourceService.removeById(id);
        return ApiResult.ok("删除成功！");
    }


    @GetMapping("/queryPage/{page}/{size}")
    @ApiOperation("分页查询")
    public ApiResult queryPage(@PathVariable Integer page ,
                               @PathVariable Integer size){
        Page<Resource> page1 = resourceService.Page(page, size);
        return ApiResult.ok(page1);

       /* Page<ResourceVO> pageList = resourceService.Page(page, size);
        PageDTO<ResourceVO> pageDTO=new PageDTO();
        pageDTO.setTotal(pageList.getTotal());
        pageDTO.setRecords(pageList.getRecords());
        return ApiResult.ok();*/
    }

}
